<template>
    <div class="e-form">
        <BillTop title="租赁验收新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.handoverLease" label-width="170px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="验收日期：" prop="acceptanceDate">
                            <el-date-picker
                                v-model="formData.handoverLease.acceptanceDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="租赁类型：" prop="leaseType">
                            <el-select v-model="formData.handoverLease.leaseType" @change="selectLeaseType">
                                <el-option :value="item.value" :label="item.label" v-for="item,index in leaseTypeList" :key="index" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                     <el-col :span="12">
                        <el-form-item label="出租单位：">
                            <el-input v-model="formData.handoverLease.leaseUnit" readonly v-if="formData.handoverLease.leaseType == '2' ">
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectLeaseUnit"></i>
                            </el-input>
                            <span v-else>{{ formData.handoverLease.leaseUnit }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="源单编号：" prop="originalNo">
                            <el-col :span="20">
                                <el-input v-model="formData.handoverLease.originalNo" readonly>
                                    <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectOriginalNo"></i>
                                </el-input>
                            </el-col>
                            <el-col :span="4" v-if="formData.handoverLease.leaseType != 1">
                                <el-link type="primary" :underline="false" @click="lookSource">查看源单</el-link>
                            </el-col>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="验收地点：" prop="acceptanceSiteDetails">
                            <SelectAddr v-model="formData.handoverLease.acceptanceSiteDetails"
                                lazy
                                showCountry
                            ></SelectAddr>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                         <el-form-item label="验收人：">
                            <span>{{ formData.handoverLease.handoverPeople }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                          <span>{{ statusLable(formData.handoverLease.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                            <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item  label="验收情况：">
                            <el-input type="textarea" v-model="formData.handoverLease.remarks" placeholder="请输入内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose"  v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/purchaseAcceptance.js'
import { mapState } from 'vuex'
import SelectAddr from '@/components/common/addrComp'
import { selectLeaseMoveEquip, selectLeaseplanContract } from '@/api/equip/popupCallback.js'
import { openAppClient, selectDataClient } from '@/utils/common'
export default {
    data () {
        return {
            res: '',
            formData: {
                //基础信息
                'handoverLease': {
                    'acceptanceDate': '',
                    'acceptanceSite': '',
                    'acceptanceSiteDetails': '',
                    'acceptanceSiteList': [],
                    'acceptanceSiteNumber': '',
                    'acceptanceSiteNumberList': [],
                    'acceptanceType': '',
                    'auditDate': '',
                    'auditor': '',
                    'auditorId': '',
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'contractId': '',
                    'contractName': '',
                    'contractNo': '',
                    'founderId': '',
                    'founderName': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'handoverPeople': '',
                    'handoverPeopleId': '',
                    'leasePlanId': '',
                    'leasePlanNo': '',
                    'leaseType': '',
                    'leaseUnit': '',
                    'leaseUnitId': '',
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'orgId': '',
                    'orgName': '',
                    'originalNo': '',
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'shortCode': '',
                    'state': 0,
                    'supplierId': '',
                    'supplierName': '',
                    'workId': '',
                    'type': '' //0合同 1计划
                },
            },
            rules: {
                acceptanceDate: [
                    { required: true, message: '请选择验收日期', trigger: 'change' },
                ],
                leaseType: [
                    { required: true, message: '请选择租赁类型', trigger: 'change'  },
                ],
                originalNo: [
                    { required: true, message: '请选择源单编号', trigger: 'change' },
                ],
                acceptanceSiteDetails: [
                    { required: true, message: '请选择验收地点', trigger: 'change' },
                ],
            },
        }
    },
    components: {
        SelectAddr
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            leaseTypeList: state=>state.equip.equipData.leaseTypeList, //租赁类型
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj ? obj.label : ''
            }
        },
    },
    created () {
    },
    mounted () {
        this.userId = localStorage.getItem('userId')
        this.userName = localStorage.getItem('userName')
        const date = new Date()
        this.formData.handoverLease.acceptanceDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
        this.formData.handoverLease.handoverPeople = this.userName
        this.formData.handoverLease.handoverPeopleId = this.userId
    },
    methods: {
        //选择原单
        selectOriginalNo () {
            if(this.formData.handoverLease.leaseType == '') {
                this.clientPop('err', '请选择租赁类型！')
                return
            }else if(this.formData.handoverLease.leaseType == 1) {
                let ids = this.formData.handoverLease.contractId ? [this.formData.handoverLease.contractId] : []
                selectLeaseMoveEquip(ids).then(res=>{ //选择设备调动
                    res.forEach(item=>{
                        this.formData.handoverLease.originalNo = item.billNo
                        this.formData.handoverLease.leaseUnit = item.transferOrg
                        this.formData.handoverLease.leaseUnitId = item.transferOrgId
                        this.formData.handoverLease.contractId = item.billId
                        this.formData.handoverLease.type = item.type
                    })
                })
            }else{
                let ids = this.formData.handoverLease.contractId ? [this.formData.handoverLease.contractId] : []
                selectLeaseplanContract(ids, this.formData.handoverLease.leaseType).then(res=>{ //选择租赁计划/合同
                    res.forEach(item=>{
                        this.formData.handoverLease.originalNo = item.billNo
                        if(this.formData.handoverLease.leaseType != 2) {
                            this.formData.handoverLease.leaseUnit = item.transferOrg
                            this.formData.handoverLease.leaseUnitId = item.transferOrgId
                        }
                        this.formData.handoverLease.contractId = item.billId
                        this.formData.handoverLease.type = item.type
                    })
                })
            }
        },
        //查看源单
        lookSource () {
            if(!this.formData.handoverLease.contractId) {
                this.clientPop('err', '请选择源单编号' )
                return
            }
            if(this.formData.handoverLease.type == 1) {
                openAppClient(`/leasePlanShow?billid=${this.formData.handoverLease.contractId}&isview=true`, '租赁计划')
            }else{
                openAppClient(`/ctEquipmentLeaseApplyShow?billid=${this.formData.handoverLease.contractId}&isview=true`, '设备租赁合同')
            }
        },
        //切换租赁类型
        selectLeaseType () {
            this.formData.handoverLease.originalNo = ''
            this.formData.handoverLease.leaseUnit = ''
            this.formData.handoverLease.leaseUnitId = ''
            this.formData.handoverLease.contractId = ''
            this.formData.handoverLease.type = ''
        },
        //为临租选择出租单位
        selectLeaseUnit () {
            const obj = {
                'businessUnitTypes': [],
                'keyword': '',
                'limit': 10,
                'orgId': '',
                'outerTypes': [],
                'page': 1,
                'states': [],
                'type': [0, 1, 2]
            }
            return selectDataClient({
                SelectServiceToken: 'GeneralSelectOuter', // 客户端对应方法名
                IsSingleMode: true, // 是否单选 true 单选 false 多选
                SelectedIDs: [this.formData.handoverLease.leaseUnitId], // 已选中id
                InitParamsStr: JSON.stringify(obj),    // 接口参数(name为模拟参数)
                Title: '选择出租单位' // 标题
            }).then(res=>{
                this.formData.handoverLease.leaseUnit = res[0].outerName
                this.formData.handoverLease.leaseUnitId = res[0].id
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.leaseAcceptanceAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/leaseAcceptanceEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/leaseAcceptanceList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
